#!/usr/bin/env groovy
@Library('CI_Script')
import com.yidao.www.*;

def java = [
    [
        'name' : 'D200',
        'branch' : 'dev-beta',
        'language' : 'java',
        'webhookUrl' : '',
        'profile' : ''
    ]
]
def robot = [
    [
        'name' : 'D200',
        'arch' : 'x86_64',
        'branch' : 'dev-beta',
        'distro' : '16.04'
    ],
    [
        'name' : 'D200',
        'arch' : 'aarch64',
        'branch' : 'dev-beta',
        'distro' : '16.04'
    ]
]

def embed = [
    [
        'name' : 'D200Embed',
        'branch' : 'master'
    ]
]

def upgradeTools = [
    [
        'name' : 'upgradeTools',
        'branch' : 'master',
        'type' : 'web',
        'robot' : 'D200'
    ]
]

def calibration = [
    'name' : 'D200Calibration',
    'branch' : 'dev-beta'
]


def builds = [:]
def build = new build()
def base = new base()
def robotBuild = new robotBuild()
def embedBuild = new embedBuild()
def toolsBuild = new toolsBuild()
def calibrationBuild = new calibrationBuild()
def downloads = []
def clean = params.CLEAN //布尔值参数，是否全量编译
def number = BUILD_NUMBER //Jenkins 内置变量 Build_number
// def robotName = params.ROBOTNAME //机器人代号 e.g. D200
// def version = params.VERSION //机器人阶段号 e.g. tr1,tr2,tr3
def majorVersion = params.MAJORVERSION
def subVersion = params.SUBVERSION
def stageVersion = params.STAGEVERSION
def dateVersion = new Date().format('MMddHH')
def letterVersion = params.LETTERVERSION
def robotDeployProfile = robotDeployProfile(robotName, number, version)
def remoteDir = robotDeployProfile.artifactory
// def robotPackage = robotDeployProfile.packageName
def robotVersion = majorVersion + '.' + subVersion + '.' + stageVersion + '.' + dateVersion + ' ' + letterVersion
def robotPackage = robotVersion + '.zip'
def localDir = robotDeployProfile.localDir
def localPackage = robotDeployProfile.localPackage
def config = robotDeployProfile.config
def branch

println '版本号:' + robotPackage

for(i=0;i<java.size();i++){
    def project = java[i]
    builds["${project.name}"] = {
        def result = build.javaManuallyBuild(project.name, project.branch, project.language, project.webhookUrl, project.profile)
        downloads.add(result)
    }
}

for(i=0;i<robot.size();i++){
    def project = robot[i]
    builds["${project.name}-${project.arch}-${project.distro}"] = {
        def result = robotBuild.robotAutoBuild(project.name, project.arch, project.branch, project.distro, clean)
        downloads.add(result)
        branch = project.branch
    }
}

for(i=0;i<embed.size();i++){
    def project = embed[i]
    builds["${project.name}"] = {
        def result = embedBuild.autoBuild(project.name, project.branch)
        downloads.add(result)
    }
}

for(i=0;i<upgradeTools.size();i++){
    def project = upgradeTools[i]
    builds["${project.name}-${project.type}"] = {
        def result = toolsBuild.autoBuild(project.name, project.branch, project.type, project.robot, branch, version, number)
        downloads.add(result)
    }
}

builds["${calibration.name}"] = {
    def result = calibrationBuild.autoBuild(calibration.name, calibration.branch)
    downloads.add(result)
}

timestamps{
    parallel builds
    println downloads
    def server = Artifactory.server 'jfrog-artifactory'
    def downloadUrl
    def srverUrl = server.url

    node(){
        for(download in downloads){
            print download
            downloadUrl = download
            print downloadUrl
            base.fileDownload(downloadUrl, localDir)
        }
        sh """
            echo "版本号: ${robotVersion}" > version.txt
        """

        base.zipPackage(robotPackage, localPackage)
        def deployTarget = base.backupFolder(remoteDir, robot, branch)
        base.fileUpload(robotPackage, deployTarget)
        def zipStorage = srverUrl + '/' + deployTarget
        currentBuild.description = """<span>存储地址:<a href=${zipStorage}>${zipStorage}</a></span>"""

        cleanWs() //清除工作空间
    }
}